package com.tuyue.police.controller.vip;

import com.tuyue.police.bean.EnterpriseBean;
import com.tuyue.police.biz.IEnterpriseBiz;
import com.tuyue.police.enums.ResultEnum;
import com.tuyue.police.exception.MyException;
import com.tuyue.police.pojo.Enterprise;
import com.tuyue.police.util.Result;
import com.tuyue.police.util.ResultUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.web.PageableDefault;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Author: 王金海
 * @Description:
 * @Date: Created by Administrator on 2018/3/14.
 * @Modified By:
 */
@RestController
@RequestMapping("vip/enterprise")
public class EnterpriseController {

    @Autowired
    private IEnterpriseBiz enterpriseBiz;

    /**
     * 新增和更新企业
     * @param enterprise
     * @return
     */
    @PostMapping("addAndUpdata")
    public Result addAndUpdata(@RequestBody Enterprise enterprise){
        if (StringUtils.isEmpty(enterprise.geteUserId())||enterprise.geteUserId()==0) {
            throw new MyException(ResultEnum.USER_NOTNULL);
        }
        Enterprise addAndUpdata = enterpriseBiz.addAndUpdata(enterprise);
        return ResultUtil.success(addAndUpdata);
    }


    /**
     * 查询企业列表带分页
     * @param userId
     * @return
     */
    @GetMapping("list/{userId}")
    public Result list(@PageableDefault(size = 10, sort = {"eId"}, direction = Sort.Direction.DESC)
                                   Pageable pageable, @PathVariable Long userId){
        Page<Enterprise> enterprises= enterpriseBiz.findAllByUserId(userId,pageable);
        return ResultUtil.success(enterprises);
    }


    /**
     * 查询企业列表
     * @param userId
     * @return
     */
    @GetMapping("listEnterprise/{userId}")
    public Result listEnterprise(@PathVariable Long userId){
        List<Enterprise> enterprises= enterpriseBiz.findAllByUserIdList(userId);
        return ResultUtil.success(enterprises);
    }

    /**
     * 通过ID查询企业详情
     * @param eId
     * @return
     */
    @GetMapping("findone/{eId}")
    public Result findone(@PathVariable Long eId){
        Enterprise enterprise= enterpriseBiz.findOne(eId);
        return ResultUtil.success(enterprise);
    }

    /**
     * 通过用户ID获取 企业树列表(企业->设备->防区)
     * @param userId
     * @return
     */
    @GetMapping("listTree/{userId}")
    public Result listTree(@PathVariable Long userId){
        List<EnterpriseBean> devices=enterpriseBiz.findTreeByUserId(userId);
        return ResultUtil.success(devices);
    }
}
